//package keter.web.servlet.wx;
//
//import java.io.IOException;
//import java.io.PrintWriter;
//
//import javax.servlet.ServletConfig;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import com.jfinal.kit.HttpKit;
//import com.jfinal.weixin.sdk.api.ApiConfig;
//import com.jfinal.weixin.sdk.api.ApiConfigKit;
//import com.jfinal.weixin.sdk.kit.SignatureCheckKit;
//import com.jfinal.weixin.sdk.msg.InMsgParser;
//import com.jfinal.weixin.sdk.msg.OutMsgXmlBuilder;
//import com.jfinal.weixin.sdk.msg.in.InMsg;
//import com.jfinal.weixin.sdk.msg.in.InTextMsg;
//import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
//
///**
// *  url: 
// *  http://shadowedge.vicp.cc/keter-app/wx
// *
// * @author  gulixing@msn.com
// * @date    Aug 31, 2015
// */
//public class WxServletJF extends HttpServlet {
//	/**
//	 * Logger for this class
//	 */
//	private static final Logger logger = LoggerFactory.getLogger(WxServletJF.class);
//	private String inMsgXml = null;		// 本次请求 xml数据
//	private InMsg inMsg = null;			// 本次请求 xml 解析后的 InMsg 对象
//
//	
//	public void init(ServletConfig config) throws ServletException{
//        ApiConfig ac = new ApiConfig();
//		// 配置微信 API 相关常量
////		ac.setToken(PropKit.get("token"));
////		ac.setAppId(PropKit.get("appId"));
////		ac.setAppSecret(PropKit.get("appSecret"));
//		ac.setToken("token");
//		ac.setAppId("wxf6bf8ca796852e7f");
//		ac.setAppSecret("9ddac60c7570fdeb49bd1e510fb65492");
//		
//		/**
//		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
//		 *  1：true进行加密且必须配置 encodingAesKey
//		 *  2：false采用明文模式，同时也支持混合模式
//		 */
//		ac.setEncryptMessage(false);
////		ac.setEncryptMessage(true);
//		ac.setEncodingAesKey("r5PrPgcmYpFRg9FQ6QdV3Ds7krCRSxs2PPU9LRsePN6");
////		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
////		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));  
//		ApiConfigKit.setThreadLocalApiConfig(ac);
//        this.init();
//    }
//	
//	
//	public void doGet(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		checkSignature(request, response);
//	}
//
//
//	private void checkSignature(HttpServletRequest request, HttpServletResponse response)
//			throws IOException {
//		logger.info("-------开始验证请求是否来自微信！");
//		//微信加密签名
//		String signature =request.getParameter("signature");
//		//时间戳 
//		String timestamp=request.getParameter("timestamp");
//		//随机数
//		String nonce=request.getParameter("nonce");
//		//随机字符串
//		String echostr=request.getParameter("echostr");
//		logger.info("---接收到的来自"+request.getRemoteHost()+",请求参数：signature:"+signature+",timestamp:"+timestamp+",nonce:"+nonce+",echostr:"+echostr);
//
//		PrintWriter out=response.getWriter();
//
//		//通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败   
//		if(signature==null||timestamp==null||nonce==null){//||echostr==null
//			out.write("you records has recorded,please leave it now !");
//			out.close();
//			out=null;
//		}else {
//			if(SignatureCheckKit.me.checkSignature(signature, timestamp, nonce)){
//				out.write(echostr);
//			}			
//		}
//		out.close();
//		out=null;
//	}
//
//	public void doPost(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
////		doGet(request, response);
//		 // 将请求、响应的编码均设置为UTF-8（防止中文乱码）   
//		request.setCharacterEncoding("UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		InMsg im = getInMsg(request);
//		if (im instanceof InTextMsg)
//			processInTextMsg((InTextMsg) im,response);
//	}
//	
//	protected void processInTextMsg(InTextMsg im, HttpServletResponse response)
//	{
//		//转发给多客服PC客户端
//		OutTextMsg outCustomMsg = new OutTextMsg(im);
//		outCustomMsg.setToUserName(im.getToUserName());
//		outCustomMsg.setFromUserName(im.getFromUserName());
//		outCustomMsg.setCreateTime(outCustomMsg.now());
//		outCustomMsg.setContent("你的消息是："+im.getContent());
//		String xml = OutMsgXmlBuilder.build(outCustomMsg);
//		logger.info("respose is: \n"+xml);
//		xml="<xml> <ToUserName> <![CDATA[444]]> </ToUserName> <FromUserName> <![CDATA[555]]> </FromUserName> <CreateTime>1441029277189</CreateTime> <MsgType> <![CDATA[text]]> </MsgType> <Content> <![CDATA[Duang~~~ your message is [44545]]]> </Content> <FuncFlag>0</FuncFlag> </xml>";
//	    print(xml,response);
//	}
//	
//	
//	public String getInMsgXml(HttpServletRequest request) {
//			inMsgXml = HttpKit.readIncommingRequestData(request);
//		return inMsgXml;
//	}
//	
//	public InMsg getInMsg(HttpServletRequest request) {
//			inMsg = InMsgParser.parse(getInMsgXml(request)); 
//		return inMsg;
//	}
//
//	//向请求端发送返回数据
//	public void print(String content, HttpServletResponse response) {
//		try {
//			response.getWriter().print(content);
//			response.getWriter().flush();
//			response.getWriter().close();
//		} catch (Exception e) {
//			logger.error("",e);
//		}
//	}
//}